const db = wx.cloud.database()
const _ = db.command
const $ = db.command.aggregate
const app = getApp()

import store from '../store/index'
const _openid = store.data.openid

const update = async (params)=>{
  return new Promise((resolve,reject)=>{
    
  db.collection('user').where({
    _openid
  }).update({
    ...params
  }).then(res=>[
    resolve(res)
  ])
  })
}


const getAvatar = async (params)=>{
//const db = wx.cloud.database()
  const openid = params

  return new Promise ((resolve,reject)=>{
   // console.log("nei"+openid)
    db.collection('user').where({
      _openid:_openid
    })
    .field({
      _openid:true,
      nickName:true,
      avatarUrl:true
    })
    .get({
      success:res=>{
        console.log(res)
        resolve({data:res.data[0]})
      },
      fali:reject
    })
    
  })
 
}

const getInfo = async (params)=>{
  var openid =params.openid
  console.log(openid)
  return new Promise ((resolve,reject)=>{
    // console.log("nei"+openid)
    wx.cloud.callFunction({
      name:"user",
      data:{action:'getInfo',openid:openid}
    }).then(res=>{
      resolve(res)
    }).catch(err=>{
      reject(err)
    })
     
   })
}

const collect = async(params)=>{
  var id = params.id
 // console.log(id)
  //console.log(_openid)
  return new Promise((reslove,reject)=>{
   return db.collection('user').where({
      _openid:_openid
    }).update({
      data:{
        collect:_.push(id)
      },
      success:res=>{
        reslove(res)
      }
    })
  })
}
const uncollect = async(params)=>{
  const id = params.id
  return new Promise((reslove,reject)=>{
   return db.collection('user').where({
      _openid:_openid
    }).update({
      data:{
        collect:_.pull(id)
      },
      success:res=>{
        reslove(res)
      }
    })
  })
}

const collectIDList = async (params)=>{
  return new Promise((reslove,reject)=>{
    db.collection('user').where({
      _openid:_openid
    })
    .field({
      collect:true
    })
    .get()
    .then(res=>{
      //console.log(res.data[0].collect)
      reslove({data:res.data[0].collect})
    })
    .catch(err=>{
      reject(err)
    })
  })
}

const collectList = async(params)=>{
  var idList =  params.list
  console.log(idList)
  return new Promise((reslove,reject)=>{
     // console.log(idList)
      let proArr = []
      for (var index= 0;index<idList.length;index++){
        proArr.push(new Promise((reslove,reject)=>{
          db.collection('contest').doc(idList[index]).get()
          .then(res=>{
            //console.log(res) 
             reslove(res.data)
          })
          .catch(err=>{
            reject(err)
          })
        }))      
      }
      Promise.all(proArr).then(res=>{
        reslove(res)
      })
  })
}
const getSkill= async function(params){
  var openid = params.openid

  return new Promise((reslove,reject)=>{
    db.collection('user').where({
      _openid:openid
    })
    .field({
        skill:true
    }).get()
    .then(res=>{
      console.log(res)
      reslove(res.data[0].skill)
    }).catch(err=>{
      reject(err)
    })
  })
  
}

export default {
  update,
  getAvatar,
  getInfo,
  collect,
  uncollect,
  collectList,
  collectIDList,
  getSkill
 
}